using System;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Serialization;

namespace Newtonsoft.Json.Converters
{
	public class RegexConverter : JsonConverter
	{
		private const string string_0 = "Pattern";

		private const string string_1 = "Options";

		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			Regex regex_ = (Regex)value;
			BsonWriter bsonWriter = writer as BsonWriter;
			if (bsonWriter != null)
			{
				method_1(bsonWriter, regex_);
			}
			else
			{
				method_2(writer, regex_, serializer);
			}
		}

		private bool method_0(RegexOptions regexOptions_0, RegexOptions regexOptions_1)
		{
			return (regexOptions_0 & regexOptions_1) == regexOptions_1;
		}

		private void method_1(BsonWriter bsonWriter_0, Regex regex_0)
		{
			string text = null;
			if (method_0(regex_0.Options, RegexOptions.IgnoreCase))
			{
				text += "i";
			}
			if (method_0(regex_0.Options, RegexOptions.Multiline))
			{
				text += "m";
			}
			if (method_0(regex_0.Options, RegexOptions.Singleline))
			{
				text += "s";
			}
			text += "u";
			if (method_0(regex_0.Options, RegexOptions.ExplicitCapture))
			{
				text += "x";
			}
			bsonWriter_0.WriteRegex(regex_0.ToString(), text);
		}

		private void method_2(JsonWriter jsonWriter_0, Regex regex_0, JsonSerializer jsonSerializer_0)
		{
			DefaultContractResolver defaultContractResolver = jsonSerializer_0.IContractResolver_0 as DefaultContractResolver;
			jsonWriter_0.WriteStartObject();
			jsonWriter_0.WritePropertyName((defaultContractResolver != null) ? defaultContractResolver.GetResolvedPropertyName("Pattern") : "Pattern");
			jsonWriter_0.WriteValue(regex_0.ToString());
			jsonWriter_0.WritePropertyName((defaultContractResolver != null) ? defaultContractResolver.GetResolvedPropertyName("Options") : "Options");
			jsonSerializer_0.Serialize(jsonWriter_0, regex_0.Options);
			jsonWriter_0.WriteEndObject();
		}

		public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
		{
			if (reader.JsonToken_0 == JsonToken.StartObject)
			{
				return method_4(reader, serializer);
			}
			if (reader.JsonToken_0 != JsonToken.String)
			{
				throw JsonSerializationException.smethod_1(reader, "Unexpected token when reading Regex.");
			}
			return method_3(reader);
		}

		private object method_3(JsonReader jsonReader_0)
		{
			string text = (string)jsonReader_0.Object_0;
			int num = text.LastIndexOf('/');
			string pattern = text.Substring(1, num - 1);
			string text2 = text.Substring(num + 1);
			RegexOptions regexOptions = RegexOptions.None;
			string text3 = text2;
			for (int i = 0; i < text3.Length; i++)
			{
				switch (text3[i])
				{
				case 'm':
					regexOptions |= RegexOptions.Multiline;
					break;
				case 'i':
					regexOptions |= RegexOptions.IgnoreCase;
					break;
				case 'x':
					regexOptions |= RegexOptions.ExplicitCapture;
					break;
				case 's':
					regexOptions |= RegexOptions.Singleline;
					break;
				}
			}
			return new Regex(pattern, regexOptions);
		}

		private Regex method_4(JsonReader jsonReader_0, JsonSerializer jsonSerializer_0)
		{
			string text = null;
			RegexOptions? regexOptions = null;
			while (jsonReader_0.Read())
			{
				switch (jsonReader_0.JsonToken_0)
				{
				case JsonToken.PropertyName:
				{
					string a = jsonReader_0.Object_0.ToString();
					if (jsonReader_0.Read())
					{
						if (string.Equals(a, "Pattern", StringComparison.OrdinalIgnoreCase))
						{
							text = (string)jsonReader_0.Object_0;
						}
						else if (string.Equals(a, "Options", StringComparison.OrdinalIgnoreCase))
						{
							regexOptions = jsonSerializer_0.Deserialize<RegexOptions>(jsonReader_0);
						}
						else
						{
							jsonReader_0.Skip();
						}
						break;
					}
					throw JsonSerializationException.smethod_1(jsonReader_0, "Unexpected end when reading Regex.");
				}
				case JsonToken.EndObject:
					if (text == null)
					{
						throw JsonSerializationException.smethod_1(jsonReader_0, "Error deserializing Regex. No pattern found.");
					}
					return new Regex(text, regexOptions ?? RegexOptions.None);
				}
			}
			throw JsonSerializationException.smethod_1(jsonReader_0, "Unexpected end when reading Regex.");
		}

		public override bool CanConvert(Type objectType)
		{
			return objectType == typeof(Regex);
		}
	}
}
